
local ClassRef = app.mvc.createRenderClass(...)
local battleCfg = battle.BattleConfig:getInstance()

local IconDir = {
	Res.meleeIconPath,
	Res.ZombieShopIcon
}

function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.BattleShopItem,false,true))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
	self.super.init(self, data)
	self:setContentSize(self._ui.Panel:getContentSize())
	self.name = self._ui.Panel:findChild("name")
	self.price = self._ui.Panel:findChild("num_gold")
	self.icon = self._ui.Panel:findChild("icon_bg/icon")
	self.btn = self._ui.Panel:findChild("Button_buy")
	self.tips = self._ui.Panel:findChild("tips")

	
	self:refresh()
end

function ClassRef:getIconDir()
	local pathDir
	if self._battleType == Enums.EBattleType.BtType_Melee then
		pathDir = Res.meleeIconPath
	elseif self._battleType == Enums.EBattleType.BtType_Zombie then
		pathDir = Res.ZombieShopIcon
	end
	return pathDir
end

function ClassRef:refresh()
	local data = self._data
		if data then
		self._battleType = data.battleType

		display.uiAddClick(self.btn, function()
			BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BUY_ITEM,{BuyItemID = data.tid, BuyNum = 1})
		end)
		local cfg = battleCfg:getItemConfig(data.tid)
		local price = cfg:getItemPrice()
		local icon = cfg:getItemIcon()

		local name = L(cfg:getItemName())
		local desc = L(cfg:getItemDesc())
		self.name:setString(name)
		self.price:setString(price)
		local pathDir = self:getIconDir()
		self.icon:loadTexture(string.format(pathDir, icon))
		self.tips:setString(desc)

		self:setSlected(false)
	end
end

function ClassRef:setSlected(bSel)
	self.btn:setBright(not bSel)
end

function ClassRef:setIndex(idx)
	self._idx = idx
end

function ClassRef:getIndex()
	return self._idx
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
